<template>
  <div class="message">
    <top-header></top-header>

    <div>
      <search-one @search="onSearch"></search-one>
    </div>

    <div class="common-list-body common-list-body-2">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <div class="list-item" v-for="(item, key) in listSource" :key="key">
            <div class="avatar-div">
              <img :src="item.sendUserPortrait ? `${$config.apiHost}/prod-api${item.sendUserPortrait}` : require(`@/assets/images/profile.jpg`)" alt="" />
            </div>
            <div class="item-info">
              <div class="content">{{ item.content }}</div>
              <div class="bottom">{{ item.sendTime }}</div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";

@Component({
  name: "Message",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class Message extends Base {
  listSource: any[] = [];
  loading = false;
  finished = false;
  refreshing = false;
  list: any[] = [];

  private pageNum = 1;
  private pageSize = 20;
  private sortBy = ""; // 排序字段
  private order = ""; // 排序顺序
  private content = ""; // 消息内容

  onSearch(event: any) {
    this.content = event;
    this.refreshing = true;
    this.onRefresh();
  }

  onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetMyMessageList(this.listParams);
      if (res && res.code === 200) {
        if (this.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  get listParams() {
    return {
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      sortBy: this.sortBy,
      order: this.order,
      content: this.content
    };
  }
}
</script>

<style lang="less" scoped>
.message {
  .common-list-body {
    background-color: white;
  }

  .list-item {
    // height: 55px;
    padding: 10px 0;
    padding-right: 20px;
    margin-left: 20px;
    border-bottom: 1px solid #e4e4e4;
    display: flex;
    &:last-child {
      border-bottom: 0;
    }

    .item-info {
      flex: 1;
      margin-left: 13px;
      color: #51555c;
      width: calc(100% - 56px);
    }

    .avatar-div {
      height: 43px;
      width: 43px;
      border-radius: 4px;
      overflow: hidden;
    }

    .bottom {
      text-align: right;
      font-size: 13px;
      color: #aaaaaa;
      margin-top: 10px;
    }

    img {
      height: 100%;
      width: 100%;
    }
  }
}
</style>
